package com.settlement.system.controller;

import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.BrandUnit;
import com.settlement.system.model.entity.common.TblWpVendorUncommonShopConfig;
import com.settlement.system.reply.ObjectDataResponse;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.IBrandUnitService;
import com.settlement.system.service.ITblWpVendorUncommonShopConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 唯品会商户不寻常店铺设置 前端控制器
 * @author Ximi
 * @since 2020-04-15
 */

@RestController
@RequestMapping("/wp/commonShopConfig")
public class TblWpVendorUncommonShopConfigController extends BaseController<TblWpVendorUncommonShopConfig> {

    @Resource
    private ITblWpVendorUncommonShopConfigService tblWpVendorUncommonShopConfigService;

    @Resource
    private IBrandUnitService brandUnitService;

    @Override
    public IBaseService init() {
        return tblWpVendorUncommonShopConfigService;
    }

    /**
     * 获取品牌部名称
     * @param tblWpVendorUncommonShopConfig
     * @throws Exception
     */
    @Override
    protected void setModelValue(TblWpVendorUncommonShopConfig tblWpVendorUncommonShopConfig) throws Exception {
        BrandUnit brandUnit = null;
        try {
            Map<String ,Object> params = new HashMap<>(16);
            if(StringUtils.isNotEmpty(tblWpVendorUncommonShopConfig.getBrandUnitNo())) {
                params.put("brandUnitNo", tblWpVendorUncommonShopConfig.getBrandUnitNo());
                brandUnit = (BrandUnit) brandUnitService.selectByParam(params).get(0);
                tblWpVendorUncommonShopConfig.setBrandUnitName(brandUnit.getName());
            }
        } catch (ServiceException e) {
            throw new Exception(e);
        }
    }

    @Override
    protected ObjectDataResponse delete(@RequestBody TblWpVendorUncommonShopConfig model, HttpServletRequest request) {
        try {
            tblWpVendorUncommonShopConfigService.updateStatus(model.getId(), 0);
            return new ObjectDataResponse(200, "删除成功");
        } catch (Exception ex) {
            return new ObjectDataResponse(500, "删除失败，请联系管理员！");
        }
    }

    @Override
    protected ObjectDataResponse insert(@RequestBody TblWpVendorUncommonShopConfig model, HttpServletRequest request) {

        try {
            if (tblWpVendorUncommonShopConfigService.selectCountByVdb(model.getVendorCode(), model.getDeliveryType(),
                    model.getBrandUnitNo()) > 0) {
                return new ObjectDataResponse(500, "数据已经存在");
            }
            model.setStatus(1);
            return super.insert(model, request);
        } catch (Exception ex) {
            return new ObjectDataResponse(500, "新增失败，请联系管理员！");
        }
    }
}

